package com.zmn.oms.third.jd_huadi.service.impl;

import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.third.jd_huadi.JdHuadiConsts;
import com.zmn.oms.third.jd_huadi.service.JdHuadiService;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import com.zmn.oms.zmn.business.interfaces.open.OrderNewNotifyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类描述：京东华帝对接的订单状态处理
 *
 * @author XieWenBing
 * @date 2020/05/21 14:24
 */
@Service("jdHuadiNotifyService")
public class JdHuadiNotifyServiceImpl implements OrderChangeNotifyService,OrderNewNotifyService{

    private static final Logger logger = LoggerFactory.getLogger(JdHuadiNotifyServiceImpl.class);

    @Autowired
    protected  JdHuadiService jdHuadiService;

    @Override
    public void notifyChange(OrderWorkLogChange orderWorkLogChange) {

        // 判断功能是否开启
        if (!JdHuadiConsts.POWER_ON) {
            logger.info("【{}】<订单状态同步> 该功能没有开启。", JdHuadiConsts.CHANNEL_NAME);
            return;
        }

        // 判断订单的录入是否不为人工录入
        if (orderWorkLogChange.getInputType() == OrderConsts.ORDER_INPUT_TYPE_MANUAL) {
            logger.info("【{}】<订单状态同步>,订单为人工录入，不处理。", JdHuadiConsts.CHANNEL_NAME);
            return;
        }

        logger.info("【{}】<订单状态同步> ,状态信息:[{}]。", JdHuadiConsts.CHANNEL_NAME,orderWorkLogChange.toString());

        int logType = orderWorkLogChange.getLogType().intValue();

        switch (logType) {

           // 派单、工程师领单、修改、上门
            case OrderLogConsts.ORDER_LOG_TYPE_DISTRIBUTE:
            case OrderLogConsts.ORDER_LOG_TYPE_UPDATE:
            case OrderLogConsts.ORDER_LOG_TYPE_TAKE:
            case OrderLogConsts.ORDER_LOG_TYPE_VISIT:
            case OrderLogConsts.ORDER_LOG_TYPE_MASTER_MODIFY_DUTY_TIME:
                jdHuadiService.syncAcceptOrder(orderWorkLogChange);
                jdHuadiService.syncOrderDutyTimeAndMaster(orderWorkLogChange);
                break;

            // 完成
            case OrderLogConsts.ORDER_LOG_TYPE_COMPLETE:
                jdHuadiService.syncFinishOrder(orderWorkLogChange);
                break;
            default:
                logger.info("【{}】<不需要同步订单状态> ", JdHuadiConsts.CHANNEL_NAME);
                return;
        }

        logger.info("【{}】<订单状态同步完成> 。", JdHuadiConsts.CHANNEL_NAME);
    }

    @Override
    public void notifyNewOrder(OrderWorkLogChange orderWorkLogChange) throws OmsBaseException {

        // 判断功能是否开启
        if (!JdHuadiConsts.POWER_ON) {
            logger.info("【{}】<订单状态同步-新订单> 该功能没有开启。", JdHuadiConsts.CHANNEL_NAME);
            return;
        }

        // 判断订单的录入是否不为人工录入
        if (orderWorkLogChange.getInputType() == OrderConsts.ORDER_INPUT_TYPE_MANUAL) {
            logger.info("【{}】<订单状态同步-新订单>,订单为人工录入，不处理。", JdHuadiConsts.CHANNEL_NAME);
            return;
        }

        // 同步接单消息给58到家直约平台
        logger.info("【{}】<订单状态同步-新订单>,订单状态:[{}] ",JdHuadiConsts.CHANNEL_NAME,orderWorkLogChange.toString());
        jdHuadiService.syncAcceptOrder(orderWorkLogChange);
    }
}
